//
//  InDownLoadSound.m
//  Inner  Voice
//
//  Created by brother on 16/3/17.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InDownLoadSound.h"

#import <FMDB.h>

#import "InSound.h"

@implementation InDownLoadSound

static FMDatabase *_dataBase = nil;

+ (NSString *)sadBox{
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"downLoadSounds.sqlite"];
}

+ (FMDatabase *)shareDataBase {
    if (_dataBase == nil) {
        NSString *path = [self sadBox];
        
        NSLog(@"播放器 %@",path);
        _dataBase = [FMDatabase databaseWithPath:path];
        
        if (_dataBase.open) {
            NSLog(@"能打开,创建成功");
            
            NSString *sql = @"CREATE TABLE IF NOT EXISTS downloadsounds (inSound_id INTEGER PRIMARY KEY AUTOINCREMENT,inSound_name TEXT,inSound_icon TEXT,inSound_Url TEXT,sound_id TEXT);";
            
            BOOL success = [_dataBase executeUpdate:sql];
            if (success) {
                NSLog(@"播放器 创建表成功");
            }else {
                NSLog(@"播放器 创建表失败");
                NSLog(@"播放器 %@",_dataBase.lastErrorMessage);
            }
        }else {
            NSLog(@"播放器 创建数据库失败");
        }
    }
    return _dataBase;
}

+ (NSMutableArray *)soundArray {
    
    NSMutableArray *soundArray = [NSMutableArray array];
    FMResultSet * result = [[self shareDataBase]executeQuery:@"SELECT * FROM downloadsounds"];
    
    while (result.next) {
        InSound *inSound = [[InSound alloc]init];
        inSound.inSound_id = (NSInteger)[result intForColumn:@"inSound_id"];
        inSound.inSound_name = [result stringForColumn:@"inSound_name"];
        inSound.inSound_icon = [result stringForColumn:@"inSound_icon"];
        inSound.inSound_Url = [result stringForColumn:@"inSound_Url"];
        inSound.sound_id = [result stringForColumn:@"sound_id"];
        
        [soundArray addObject:inSound];
    }
    return soundArray;
}

+ (BOOL)insertInToSound:(InSound *)sound {
    
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM downloadsounds WHERE sound_id = ?",sound.sound_id];
    
    if(result.next) {
        NSLog(@"播放器 已经有此数据");
        return  NO;
    }else {
        
        BOOL success = [[self shareDataBase]executeUpdate:@"INSERT INTO downloadsounds(inSound_name,inSound_icon,inSound_Url,sound_id) VALUES (?, ?,?,?)",sound.inSound_name,sound.inSound_icon,sound.inSound_Url,sound.sound_id];
        if (success) {
            NSLog(@"播放器 插入成功");
        }else {
            NSLog(@"播放器 插入失败");
            NSLog(@"播放器 %@",[self shareDataBase].lastErrorMessage);
        }
        return success;
    }
}

+ (BOOL)deleteSound:(InSound *)sound {
    
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM downloadsounds WHERE sound_id = ?",sound.sound_id];
    
    if(!result.next) {
        NSLog(@"播放器 无此数据");
        return  NO;
    }else {
        BOOL success = [[self shareDataBase]executeUpdate:@"DELETE FROM downloadsounds WHERE sound_id = ?",sound.sound_id];
        if (success) {
            NSLog(@"播放器 删除成功");
        }else {
            NSLog(@"播放器 删除失败");
            NSLog(@"播放器 %@",[self shareDataBase].lastErrorMessage);
        }
        return success;
    }
}

+ (void)closeDataBase {
    
    [[self shareDataBase]close];
}

@end
